package deom9;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-04-12
 * Time: 7:18
 */
class ListNode{
    int val;
    ListNode next = null;
    public ListNode(int val){
        this.val = val;
    }
}
public class Test01 {
    public ListNode FindFirstCommomNode(ListNode pHead1,ListNode pHead2){
        ListNode p1 = pHead1;
        ListNode p2 = pHead2;
        int len1 = 0;
        int len2 = 0;
        while (p1 != null){
            p1  = p1.next;
            len1++;
        }
        while (p2 != null){
            p2 = p2.next;
            len2++;
        }
        int len = len1 - len2;
        p1 = pHead1;
        p2 = pHead2;
        if(len < 0){
            p1 = pHead2;
            p2 = pHead1;
            len = len2- len1;
        }
        while (len > 0){
            p1 = p1.next;
            len --;
        }
        while (p1 != p2){
            p1 = p1.next;
            p2 = p2.next;
        }
        if(p1 == null){
            return null;
        }
        return p1;

    }
}
